package com.ts.system.util;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.ts.animal.domain.ExWarningSetDO;
import com.ts.common.config.TsConfig;
import com.ts.common.domain.DictDO;
import com.ts.system.domain.DeptDO;
import com.ts.system.domain.SystemParamsDO;
import com.ts.system.domain.UserDO;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import java.util.Map;

public class SystemUtil {
    private static Map<Long, UserDO> users;
    private static Map<Long, DeptDO> idDepts;
    private static Map<String, DeptDO> appidDepts;
    private static Map<String, SystemParamsDO> systemParams;
    private static ThymeleafViewResolver viewResolver;
    private static Map<Object, Map<Object, DictDO>> dicts;
    private static Map<String, ExWarningSetDO> warningSets;

    /**
     * 系统所有用户
     *
     * @return
     */
    public static Map<Long, UserDO> getUsers() {
        return users;
    }

    public static void setUsers(Map<Long, UserDO> users) {
        SystemUtil.users = users;
        Map<String, Object> vars = Maps.newHashMap();
        Object obj = JSON.toJSON(users);
        vars.put("sysUsers", obj);
        viewResolver.setStaticVariables(vars);
    }

    /**
     * 系统所有部门
     *
     * @return
     */
    public static Map<Long, DeptDO> getIdDepts() {
        return idDepts;
    }

    public static Map<String, DeptDO> getAppidDepts() {
        return appidDepts;
    }

    public static void setIdDepts(Map<String, Object> depts) {
        SystemUtil.idDepts = (Map<Long, DeptDO>) depts.get("id");
        SystemUtil.appidDepts = (Map<String, DeptDO>) depts.get("appid");
        Map<String, Object> vars = Maps.newHashMap();
        Object obj = JSON.toJSON(idDepts);
        vars.put("sysDepts", obj);
        viewResolver.setStaticVariables(vars);
    }

    /**
     * 系统所有系统参数
     *
     * @return
     */
    public static Map<String, SystemParamsDO> getSystemParams() {
        return systemParams;
    }

    public static void setSystemParams(Map<String, SystemParamsDO> systemParams) {
        SystemUtil.systemParams = systemParams;
        Map<String, Object> vars = Maps.newHashMap();
        Object obj = JSON.toJSON(systemParams);
        vars.put("sysSystemParams", obj);
        viewResolver.setStaticVariables(vars);
    }

    public static Map<String, ExWarningSetDO> getWarningSets() {
        return warningSets;
    }

    public static void setWarningSets(Map<String, ExWarningSetDO> exWarningSetDO) {
        SystemUtil.warningSets = exWarningSetDO;
    }

    public static ThymeleafViewResolver getViewResolver() {
        return viewResolver;
    }

    public static void setViewResolver(ThymeleafViewResolver viewResolver) {
        //设置全局变量
        viewResolver.addStaticVariable("maxOrg", Long.valueOf(TsConfig.getProperty("ts.manage.maxOrg")));
        SystemUtil.viewResolver = viewResolver;
    }

    //系统所有静态数据字典
    public static Map<Object, Map<Object, DictDO>> getDicts() {
        return dicts;
    }

    public static void setDicts(Map<Object, Map<Object, DictDO>> dicts) {
        SystemUtil.dicts = dicts;
        Map<String, Object> vars = Maps.newHashMap();
        Object obj = JSON.toJSON(dicts);
        vars.put("sysDicts", dicts);
        viewResolver.setStaticVariables(vars);
    }

}
